<template>
  <div class="upload-wrap">
    <el-upload
      class="avatar-uploader"
      action="https://jsonplaceholder.typicode.com/posts/"
      :show-file-list="false"
      :before-upload="beforeAvatarUpload"
      :http-request="upload"
      @mouseenter.native="coverShow = imageUrl ? true: false"
      @mouseleave.native="coverShow = false"
    >
      <img
        v-if="imageUrl"
        :src="imageUrl"
        class="avatar"
      />
      <i v-else class="el-icon-plus avatar-uploader-icon"></i>
      <div v-if="coverShow" class="cover">
        <el-icon @click.native.stop="iconClick" class="el-icon-zoom-in"></el-icon>
        <el-icon @click.native.stop="deleteClick" class="el-icon-delete-solid"></el-icon>
      </div>
    </el-upload>

    <el-dialog :visible.sync="showDialog" title="预览图片">
      <img style="width: 100%; display:block;margin:0 auto;" :src="imageUrl" alt="">
    </el-dialog>
  </div>
</template>


<script>
// 导入
import COS from "cos-js-sdk-v5";
// 实例化一个操作腾讯云的对象
let cos = new COS({
  // 密钥id
  SecretId: "AKIDb2D7mBJAk48vRhfMmvnOhjLhmbCXbOvB",
  // 密钥key
  SecretKey: "0jALFrCI8hz0kDOIwJOCjzxe5F8k59wS",
});

export default {
  data() {
    return {
      imageUrl:'',
      coverShow: false,
      showDialog: false,
    };
  },
  methods: {

    deleteClick () {

      this.imageUrl = ''
      this.coverShow = false
    },
    iconClick () {
      console.log('....')
      this.showDialog = true
    },
    upload(params) {
      cos.putObject(
        {
          // 存储桶名字
          Bucket: "hmhr72-1306801437" /* 必须 */,
          Region: "ap-guangzhou" /* 存储桶所在地域，必须字段 */,
          // key就是你上传后的文件名
          Key: params.file.name /* 必须 */,
          StorageClass: "STANDARD",
          Body: params.file, // 上传文件对象
        },
        // 上传成功或失败会调用的回调函数
        // 如果err有值代表失败了，如果没有值代表成功了
        (err, data) => {
          if (!err) {
            // 上传成功后的图片地址在data.Location
            // console.log(data.Location);
            this.imageUrl = "http://" + data.Location;
          }
        }
      );
    },

    // 上传之前的回调函数
    beforeAvatarUpload(file) {
      // 判断这个文件类型是否为jpg格式
      const isJPG = file.type === "image/jpeg" || file.type === "image/png" || file.type === "image/gif";
      // 判断这个文件大小是否超过2M
      const isLt2M = file.size / 1024 / 1024 < 2;

      // 如果不是JPG
      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }

      // 如果超过2M
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }

      // 如果都满足了返回一个true，返回true就代表继续去帮你做上传
      // 返回false就不上传
      return isJPG && isLt2M;
    },
  },
};
</script>

<style>
.avatar-uploader .el-upload:hover .cover i {
  font-size: 30px;
  margin-right: 10px;
  color: #0094ff;
}
.cover {
  width: 178px;
  height: 178px;
  background-color: rgba(0, 0, 0, 0.4);
  position: absolute;
  left: 0;
  top: 0;
  display: flex;
  justify-content: center;
  align-items: center;
}

.avatar-uploader {

  width: 178px;
  height: 178px;
}
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  width: 178px;
  height: 178px;
}
.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
